package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.ReasonCancel;



public class ReasonCancelLogic extends AbstractBL {

	public ReasonCancel GetReasonCancel(long id)
	{
		Key key = KeyFactory.createKey(EntityName.ReasonCancel, id);
		try {
			Entity lang = datastore.get(key);
			ReasonCancel c = new ReasonCancel();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public List<ReasonCancel> GetAllReasonCancel()
	{
		Query q = new Query(EntityName.ReasonCancel);
		
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filter = CompositeFilterOperator.and(filterStatus, filterAvaliable);
		
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	
	public List<ReasonCancel> GetList(List<Entity> entities)
	{
		List<ReasonCancel> list = new ArrayList<ReasonCancel>();
		for(Entity e : entities)
		{
			ReasonCancel a = new ReasonCancel();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public ReasonCancel AddReasonCancel(String reason,String description) throws EntityNotFoundException{
		ReasonCancel rc = new ReasonCancel();
		rc.setReason(reason);
		rc.setDescription(description);
		rc.setAvaliable(true);
		rc.setStatus(true);
		
		Key k = datastore.put(rc.getEntity());
		rc.setId(k.getId());
		rc.setKey(k);

		return rc;
	}
	
	public ReasonCancel EditReasonCancel(long id,String reason,String description) throws EntityNotFoundException{
		
		ReasonCancel rc = GetReasonCancel(id);
		
		rc.setReason(reason);
		rc.setDescription(description);
		rc.setAvaliable(true);
		rc.setStatus(true);
		Key	k=datastore.put(rc.getEntity());
		Entity e = datastore.get(k);
		
		rc = new ReasonCancel();
		rc.setEntity(e);

		return rc;
	}
	
	public ReasonCancel DeleteReasonCancel(long id) throws EntityNotFoundException{
		ReasonCancel rc = GetReasonCancel(id);
		rc.setAvaliable(false);
		rc.setStatus(false);
		
		Key k = datastore.put(rc.getEntity());
		rc.setId(k.getId());
		rc.setKey(k);

		return rc;
	}
	
	public ReasonCancel AddReasonCancelSample() {
		
		String[] reason = new String[] { "Weather Conditions", "Transporttarion Deleyed", "Political Unrest"
				,"Non Guaranteed Reeservation", "No Show", "No Reason", "Natural Disaster"
				, "Deposit Not Received","Conference Cancalled" };
		String[] description =  new String[] { "Weather Conditions", "Transporttarion Deleyed", "Political Unrest"
				,"Non Guaranteed Reeservation", "No Show", "No Reason", "Natural Disaster"
				, "Deposit Not Received","Conference Cancalled" };
	

		ReasonCancel reasoncancel = new ReasonCancel();
		for (int i = 0; i < reason.length; i++) {
			reasoncancel.setReason(reason[i]);
			reasoncancel.setDescription(description[i]);
			reasoncancel.setStatus(true);
			reasoncancel.setAvaliable(true);
			Key k = datastore.put(reasoncancel.getEntity());
			reasoncancel.setId(k.getId());
			reasoncancel.setKey(k);

		}
		return null;
	}
	
}
